package com.boot.security.server.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.boot.security.server.dao.SysSmsDao;
import com.boot.security.server.model.mffl.SysSms;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SmsUtils {


    static final String product = "Dysmsapi";// 短信API产品名称

    static final String domain = "dysmsapi.aliyuncs.com"; // 短信API产品域名

    static final String accessKeyId = "LTAIoDs7fxPCeShX";

    static final String accessKeySecret = "gl2cO0kQyJ0xpua0qj7TX02ixBI538";

    static final String signName = "智慧社区";//短信签名

    static final String templateCode = "SMS_167051115";//短信模板ID

    /**
     * 发送短信
     *
     * @param telphone 手机号
     * @param content  内容
     * @param type     类型
     * @param code
     * @return
     * @throws ClientException
     */
    public static String sendSms(SysSmsDao smsDao,String telphone, String content, String type, String code) {
        SendSmsResponse response = null;
        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setMethod(MethodType.POST);
            request.setPhoneNumbers(telphone);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);

            if (type.equals("1")) {//验证码
                request.setTemplateParam("{\"code\":\"" + code + "\"}");
            } else if (type.equals("2")) {//业务通知
                request.setTemplateParam("{\"content\":\"" + content + "\", \"code\":\"" + code + "\"}");
            } else if (type.equals("3")) {//登录账号通知
                request.setTemplateParam("{\"content\":\"" + content + "\", \"code\":\"" + code + "\"}");
            }
            response = acsClient.getAcsResponse(request);
            if (response.getCode() != null && response.getCode().equals("OK")) {
                System.out.println("发送成功！");
            } else {
                System.out.println("发送失败！");
            }
            addSms(smsDao, telphone, content, code, response.getMessage(), response.toString());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response.getMessage();
    }

    /**
     * 添加短信
     */
    public static void addSms(SysSmsDao smsDao,String telphone, String content, String code, String status, String result) {
        SysSms sms = new SysSms();
        sms.setPhone(telphone);
        sms.setCode(code);
        sms.setContext(content);
        sms.setStatus(status);
        sms.setResult(result);
        sms.setSendtime(new Date());
        smsDao.save(sms);
    }

    /**
     * 生成6位随机数验证码
     * @return
     */
    public static String vcode(){
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int)(Math.random() * 9);
        }
        return vcode;
    }

    {
/*    // 随机验证码
    private int code;

    public int getCode() {
        return code;
    }

    public void setCode() {
        code = (int) ((Math.random() * 9 + 1) * 100000); // 每次调用生成一次6位数的随机数
    }*/
    }

    //手机号验证
    //验证手机号
    public static boolean isnumber(String registrant_phone) {
        boolean error;
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(registrant_phone);  //registrant_phone  ====  电话号码字段
        boolean isMatch = m.matches();
        if (!isMatch) {
            error = false;
        } else {
            error = true;
        }
        return error;
    }
}